package com.hph.health.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hph.health.common.ErrorCode;
import com.hph.health.constant.CommonConstant;
import com.hph.health.exception.BusinessException;
import com.hph.health.model.dto.product.ProductQueryRequest;
import com.hph.health.model.entity.Product;
import com.hph.health.model.entity.User;
import com.hph.health.service.ProductService;
import com.hph.health.mapper.ProductMapper;
import com.hph.health.utils.SqlUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

/**
* @author 19378
* @description 针对表【product(商品表)】的数据库操作Service实现
* @createDate 2024-03-27 20:32:29
*/
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product>
    implements ProductService{

    @Override
    public QueryWrapper<Product> getQueryWrapper(ProductQueryRequest productQueryRequest) {
        if (productQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "请求参数为空");
        }
        String productName = productQueryRequest.getProductName();
        String brandName = productQueryRequest.getBrandName();
        BigDecimal price = productQueryRequest.getPrice();
        BigDecimal cost = productQueryRequest.getCost();
        String category = productQueryRequest.getCategory();
        Integer isGroup = productQueryRequest.getIsGroup();
        String description = productQueryRequest.getDescription();
        String merchantId = productQueryRequest.getMerchantId();
        String supplierName = productQueryRequest.getSupplierName();
        Date productionDate = productQueryRequest.getProductionDate();
        Integer shelfLife = productQueryRequest.getShelfLife();
        String sortField = productQueryRequest.getSortField();
        String sortOrder = productQueryRequest.getSortOrder();
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(productName), "product_name", productName);
        queryWrapper.like(StringUtils.isNotBlank(brandName), "brand_name", brandName);
        queryWrapper.eq(price != null, "price", price);
        queryWrapper.eq(cost != null, "cost", cost);
        queryWrapper.like(StringUtils.isNotBlank(category), "category", category);
        queryWrapper.eq(isGroup != null, "is_group", isGroup);
        queryWrapper.like(StringUtils.isNotBlank(description), "description", description);
        queryWrapper.like(StringUtils.isNotBlank(merchantId), "merchant_id", merchantId);
        queryWrapper.like(StringUtils.isNotBlank(supplierName), "supplier_name", supplierName);
        queryWrapper.eq(productionDate != null, "production_date", productionDate);
        queryWrapper.eq(shelfLife != null, "shelf_life", shelfLife);
        queryWrapper.orderBy(SqlUtils.validSortField(sortField), sortOrder.equals(CommonConstant.SORT_ORDER_ASC),
                sortField);
        return queryWrapper;
    }
}




